package com.example.demo.controller;

import com.example.demo.model.Product;
import com.example.demo.dto.ResponseResult; // 导入您的ResponseResult
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/products")
@CrossOrigin(origins = "*")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping
    public ResponseResult<List<Product>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseResult.success(products);
    }

    @GetMapping("/{id}")
    public ResponseResult<Product> getProductById(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        if (product != null) {
            return ResponseResult.success(product);
        } else {
            return ResponseResult.error(404, "产品不存在");
        }
    }

    @PostMapping
    public ResponseResult<String> createProduct(@RequestBody Product product) {
        try {
            productService.createProduct(product);
            return ResponseResult.success("产品创建成功");
        } catch (Exception e) {
            return ResponseResult.error("创建失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResponseResult<String> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        try {
            product.setId(id);
            productService.updateProduct(product);
            return ResponseResult.success("产品更新成功");
        } catch (Exception e) {
            return ResponseResult.error("更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseResult<String> deleteProduct(@PathVariable Long id) {
        try {
            productService.deleteProduct(id);
            return ResponseResult.success("产品删除成功");
        } catch (Exception e) {
            return ResponseResult.error("删除失败: " + e.getMessage());
        }
    }

    @GetMapping("/search")
    public ResponseResult<List<Product>> searchProducts(@RequestParam String name) {
        List<Product> products = productService.searchProductsByName(name);
        return ResponseResult.success(products);
    }

    @GetMapping("/category/{category}")
    public ResponseResult<List<Product>> getProductsByCategory(@PathVariable String category) {
        List<Product> products = productService.getProductsByCategory(category);
        return ResponseResult.success(products);
    }

    @PatchMapping("/{id}/stock")
    public ResponseResult<String> updateStock(@PathVariable Long id, @RequestParam Integer stock) {
        try {
            productService.updateProductStock(id, stock);
            return ResponseResult.success("库存更新成功");
        } catch (Exception e) {
            return ResponseResult.error("库存更新失败: " + e.getMessage());
        }
    }
}